<?php

namespace MoChat\App\Sales\Action\Task;

use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\Middlewares;
use Hyperf\HttpServer\Annotation\RequestMapping;
use MoChat\App\Common\Middleware\DashboardAuthMiddleware;
use MoChat\App\Sales\Contract\SalesClientTaskEmployeeContract;
use MoChat\App\Sales\Contract\SalesTaskContract;
use MoChat\App\Sales\Contract\SalesTaskEmployeeContract;
use MoChat\App\Sales\Contract\WorkDepartmentContract;
use MoChat\App\Sales\Contract\WorkEmployeeContract;
use MoChat\App\Sales\Service\WorkDepartmentService;
use MoChat\App\WorkContact\Action\Sidebar\EmployeeContact;
use MoChat\Framework\Action\AbstractAction;
use MoChat\Framework\Constants\ErrorCode;
use MoChat\Framework\Exception\CommonException;
use MoChat\Framework\Request\ValidateSceneTrait;

/**
 * 销售任务详情-员工列表
 * @Controller
 */
class Employee extends AbstractAction
{
    use ValidateSceneTrait;

    /**
     * @Inject
     * @var WorkEmployeeContract
     */
    protected $workEmployeeContract;

    /**
     * @Inject
     * @var SalesTaskEmployeeContract
     */
    protected $salesTaskEmployeeService;

    /**
     * @Inject
     * @var WorkDepartmentContract
     */
    protected $workDepartmentService;

    /**
     * @Middlewares({
     *     @Middleware(DashboardAuthMiddleware::class),
     * })
     * @RequestMapping(path="/dashboard/salesTask/employee", methods="GET")
     */
    public function handle()
    {
        $user = user();
        // 判断用户绑定企业信息
        if (!isset($user['corpIds']) || count($user['corpIds']) !== 1) {
            throw new CommonException(ErrorCode::INVALID_PARAMS, '未选择登录企业，不可操作');
        }
        $data = $this->request->all();
        $this->validated($data);
        $list = $this->salesTaskEmployeeService->getListSalesTaskEmployee([
            ['sales_task_id', '=', $data['id']]
        ], [
            'id',
            'start_time',
            'end_time',
            'work_department_id',
            'work_employee_id',
            'sales_task',//销售任务
            'progress',//完成进度
        ]);

        if (!empty($list['data'])) {
            $workEmployeeIds = array_column($list['data'], 'workEmployeeId');
            $workDepartmentIds = array_column($list['data'], 'workDepartmentId');
            $workEmployee = $this->workEmployeeContract->getAllWorkEmployeeByIds($workEmployeeIds);
            $workDepartment = $this->workDepartmentService->getAllWorkDepartmentById($workDepartmentIds);
            $workEmployee = empty($workEmployee) ? [] : array_column($workEmployee, null, 'id');
            $workDepartment = empty($workDepartment) ? [] : array_column($workDepartment, null, 'id');

            foreach ($list['data'] as &$datum) {
                $datum['employeeName'] = $workEmployee[$datum['workEmployeeId']]['name'] ?? '';
                $datum['deptName'] = $workDepartment[$datum['workDepartmentId']]['name'] ?? '';
                unset($datum['workDepartmentId'], $datum['workEmployeeId']);
            }
            unset($datum);
        }
        return $list;
    }

    /**
     * 验证规则.
     */
    protected function rules(): array
    {
        return [
            'id' => 'required|integer',
        ];
    }

    /**
     * 获取已定义验证规则的错误消息.
     */
    protected function messages(): array
    {
        return [
            'id.required' => '销量id未传递',
            'id.integer' => '销量id参数错误',
        ];
    }
}